package xdu.lz.stage1_basic.chapter4_threadException;
//当程序发生异常或者被杀死，要调用钩子函数，释放资源
//Linux下 kill port可以释放资源，但是kill -9 port没有机会，直接被杀死
public class HookFunction {

    public static void main(String[] args) {
        Runtime.getRuntime().addShutdownHook(new Thread(()->{
            System.out.println("the app will exit");
            notifyAndRelease();
        }));
        int i = 0;
        while (true){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(++i > 20000){
                throw new RuntimeException("error");
            }
        }
    }

    private static void notifyAndRelease() {
        System.out.println("notify admin to release file,program and so on");
    }
}
